local scx,scy = guiGetScreenSize()
local px = scx/1920
local py = scy/1080

local sizeX,sizeY = 350*px,350*px
local posX,posY = scx-sizeX,scy-sizeY
local czas = getTickCount()
local czasp = getTickCount()

mojeW,mojeH = 1920,1080
sW,sH = guiGetScreenSize()
sx, sy = (sW/mojeW), (sH/mojeH)

liczniki={
	["fiolet"]=true, --
	["czerw"]=true, --
	["nieb"]=true, --
	["pom"]=true, --
	["roz"]=true, --
	["turkus"]=true, -- 
	["zielone"]=true, --
	["zolty"]=true, --
	["blekit"]=true, --
	["czarny"]=true, --
	["default"]=true, --

}

licznikiRGB = {
	["czerw"]={255,0,0},
	["nieb"]={0,0,255},
	["zolty"]={255,255,0},
	["roz"]={255,0,255},
	["turkus"]={0,255,255},
	["pom"]={255,96,0},
	["fiolet"]={138,0,255},
	["zielone"]={3,167,0},
	["blekit"]={0,92,255},
	["czarny"]={0,0,0},
	["default"]={255,255,255},
}
function createIcons()
licznik = dxCreateTexture('zasoby/licznik.png', 'argb', false, 'clamp')
strzalkalicznik = dxCreateTexture('zasoby/strzalkalicznik.png', 'argb', false, 'clamp')
benzyna = dxCreateTexture('zasoby/benzyna.png', 'argb', false, 'clamp')
benzyna2 = dxCreateTexture('zasoby/benzyna2.png', 'argb', false, 'clamp')
strzalkabenzyna = dxCreateTexture('zasoby/strzalkabenzyna.png', 'argb', false, 'clamp')
swiatla = dxCreateTexture('zasoby/swiatla.png', 'argb', false, 'clamp')
drzwi = dxCreateTexture('zasoby/drzwi.png', 'argb', false, 'clamp')
silnik = dxCreateTexture('zasoby/silnik.png', 'argb', false, 'clamp')
end

function destroyIcons()
if isElement(licznik) then
destroyElement(licznik)
destroyElement(strzalkalicznik)
destroyElement(benzyna)
destroyElement(benzyna2)
destroyElement(strzalkabenzyna)
destroyElement(swiatla)
destroyElement(drzwi)
destroyElement(silnik)
end
end


local font3 = dxCreateFont( "zasoby/AEROMATICSITALIC.ttf",50*sx )
local font4 = dxCreateFont( "zasoby/AEROMATICSITALIC.ttf",18*sx )

function math.lerp(a, b, k)
	local result = a * (1-k) + b * k
	if result >= b then
		result = b
	elseif result <= a then
		result = a
	end
	return result
end

local alpha = 255
local side = true
local pulsing = true


function drawSpeedometer()
	local veh = getPedOccupiedVehicle(localPlayer)
	if veh then
		if not getElementData(veh, "vehicle:data") then
			local rotation = math.floor(((270/9800)* getVehicleRPM(getPedOccupiedVehicle(getLocalPlayer()))) + 0.5)
			if rotation >= 180 then rotation = math.random(179,200) end
			dxDrawText(string.format("%01d", getElementSpeed(veh)),1710*sx*2,950*sy*2,100*sx,50*sy,tocolor(255,255,255),1,font3,"center","center")
			dxDrawImage(1570*sx,730*sy,350*sx,350*sy,strzalkalicznik,rotation,0,0,tocolor(100,0,255)) -- -150 to 90
			dxDrawImage(1570*sx,730*sy,350*sx,350*sy,licznik,0,0,0,tocolor(255,255,255,255))
		else
		local data = getElementData(veh, "vehicle:data")
        local num = data.mileage or 0
		if data.fuel and tonumber(data.fuel) < 0.1 then setVehicleEngineState(veh,false) end
		local mileage = ""..math.floor(num)
		repeat mileage = "0"..mileage until #mileage >= 7	
		local speedx, speedy, speedz = getElementVelocity ( veh )
		local kmh = math.ceil(((speedx^2+speedy^2+speedz^2)^(0.5))*115)
		local rotation = math.floor(((270/9800)* getVehicleRPM(getPedOccupiedVehicle(getLocalPlayer()))) + 0.5)
		if rotation >= 180 then rotation = math.random(179,200) end
		local cfuel =data.fuel or 25
		local mfuel = (data.tank or 100)
		local fuellevel = cfuel/mfuel*15
		if fuellevel >= 100 then fuellevel = 100 end
		local rotation2 = math.lerp(-220,20,cfuel/mfuel)
		if rotation2 <= -190 then pulsing = true else pulsing = false end
		local rgb= data.colorspeedo
		if rgb then
		if licznikiRGB[rgb] then rgb=licznikiRGB[rgb] else rgb = split(tostring(rgb),",") end
		else
		rgb = tostring(255,255,255)
		end
		local gear = getVehicleCurrentGear( veh )
		if gear == 0 then
			if kmh <= 1 then
				gear = "N"
			else
				gear = "R"
			end 
		elseif gear == 1 then
			if kmh <= 2 then
				gear = "N"
			end
		end
		dxDrawText(string.format("%01d", getElementSpeed(veh)),1710*sx*2,950*sy*2,100*sx,50*sy,tocolor(255,255,255),1,font3,"center","center")
		dxDrawText(mileage,1725*sx*2,1010*sy*2,100*sx,50*sy,tocolor(255,255,255),1,font4,"center","center") 
		dxDrawImage(1570*sx,730*sy,350*sx,350*sy,strzalkalicznik,rotation,0,0,tocolor(100,0,255)) -- -150 to 90
		dxDrawImage(1570*sx,730*sy,350*sx,350*sy,licznik,0,0,0,tocolor(tonumber(rgb[1]) or 255,tonumber(rgb[2]) or 255,tonumber(rgb[3]) or 255,tonumber(rgb[4])))

		if data.id then

		if pulsing then
			if side then alpha = alpha + 5 else alpha = alpha -5 end
			if alpha <= 0 then side = true elseif alpha >= 255 then side = false end
			dxDrawImage (1370*sx,810*sy,350*sx,350*sy,benzyna2,0,0,0,tocolor(200,0,0,alpha))
		end
		end
				if data.id then

		if mfuel == 25 then
		dxDrawImage(1370*sx,800*sy,350*sx,350*sy,strzalkabenzyna,145+9.3*cfuel,0,0,tocolor(100,0,255)) -- -150 to 90
		elseif mfuel == 35 then
		dxDrawImage(1370*sx,800*sy,350*sx,350*sy,strzalkabenzyna,145+6.65*cfuel,0,0,tocolor(100,0,255)) -- -150 to 90
		elseif mfuel == 50 then
		dxDrawImage(1370*sx,800*sy,350*sx,350*sy,strzalkabenzyna,145+4.65*cfuel,0,0,tocolor(100,0,255)) -- -150 to 90
		else
		dxDrawImage(1370*sx,800*sy,350*sx,350*sy,strzalkabenzyna,145+9.535*cfuel,0,0,tocolor(100,0,255)) -- -150 to 90
		end
		dxDrawImage(1370*sx,810*sy,350*sx,350*sy,benzyna,0,0,0,tocolor(tonumber(rgb[1]) or 255,tonumber(rgb[2]) or 255,tonumber(rgb[3]) or 255,tonumber(rgb[4])))
end
		
		if data.id then
		dxDrawText(""..(string.format("%.1f", cfuel)).."/"..mfuel,1495*sx*2,1015*sy*2,100*sx,50*sy,tocolor(255,255,255),0.75,font4,"center","center")
        end


		if getVehicleEngineState( veh ) then
			dxDrawImage(1635*sx,710*sy,512*sx,512*sy,silnik,0,0,0,tocolor(0,255,0))
		else
			dxDrawImage(1635*sx,710*sy,512*sx,512*sy,silnik,0,0,0,tocolor(255,255,255))

		end

		if getVehicleOverrideLights( veh ) == 2 then
		    dxDrawImage(1635*sx,740*sy,512*sx,512*sy,swiatla,0,0,0,tocolor(0,255,0))
			
		elseif getVehicleOverrideLights( veh ) == 1 then
			dxDrawImage(1635*sx,740*sy,512*sx,512*sy,swiatla,0,0,0,tocolor(255,255,255))

		else
			local h,m = getTime()
			if h >= 7 and h <= 21 then
				dxDrawImage(1635*sx,740*sy,512*sx,512*sy,swiatla,0,0,0,tocolor(255,255,255))

			else
				dxDrawImage(1635*sx,740*sy,512*sx,512*sy,swiatla,0,0,0,tocolor(200,0,0))

			end
		end

		if isVehicleLocked(veh) then
			dxDrawImage(1635*sx,770*sy,512*sx,512*sy,drzwi,0,0,0,tocolor(200,0,0))
		else
			dxDrawImage(1635*sx,770*sy,512*sx,512*sy,drzwi,0,0,0,tocolor(0,255,0))

		end
	end
	end
end

addEventHandler("onClientVehicleEnter", getRootElement(),
    function(thePlayer, seat)
        if thePlayer == getLocalPlayer() then
			if not isEventHandlerAdded( 'onClientRender', root, drawSpeedometer) then
			createIcons()
            addEventHandler("onClientRender",root,drawSpeedometer)
			end
        end
    end
)
addEventHandler("onClientVehicleExit", getRootElement(),
    function(thePlayer, seat)
        if thePlayer == getLocalPlayer() then
            removeEventHandler("onClientRender",root,drawSpeedometer)
			destroyIcons()
        end
    end
)


function getElementSpeed(element,unit)
	if (unit == nil) then unit = 0 end
	if (isElement(element)) then
		local x,y,z = getElementVelocity(element)
		if (unit=="mph" or unit==1 or unit =='1') then
			return (x^2 + y^2 + z^2) ^ 0.5 * 100
		else
			return (x^2 + y^2 + z^2) ^ 0.5 * 160
		end
	else
		return false
	end
end

function getVehicleRPM(vehicle)
local vehicleRPM = 0
    if (vehicle) then  
        if (getVehicleEngineState(vehicle) == true) then
            if getVehicleCurrentGear(vehicle) > 0 then             
                vehicleRPM = math.floor(((getElementSpeed(vehicle, "kmh")/getVehicleCurrentGear(vehicle))*180) + 0.5) 
                if (vehicleRPM < 650) then
                    vehicleRPM = math.random(650, 750)
                elseif (vehicleRPM >= 9800) then
                    vehicleRPM = math.random(9800, 9900)
                end
            else
                vehicleRPM = math.floor((getElementSpeed(vehicle, "kmh")*180) + 0.5)
                if (vehicleRPM < 650) then
                    vehicleRPM = math.random(650, 750)
                elseif (vehicleRPM >= 9800) then
                    vehicleRPM = math.random(9800, 9900)
                end
            end
        else
            vehicleRPM = 0
        end
        return tonumber(vehicleRPM)
    else
        return 0
    end
end

function isEventHandlerAdded( sEventName, pElementAttachedTo, func )
    if type( sEventName ) == 'string' and isElement( pElementAttachedTo ) and type( func ) == 'function' then
        local aAttachedFunctions = getEventHandlers( sEventName, pElementAttachedTo )
        if type( aAttachedFunctions ) == 'table' and #aAttachedFunctions > 0 then
            for i, v in ipairs( aAttachedFunctions ) do
                if v == func then
                    return true
                end
            end
        end
    end
    return false
end